 function editUser(index){
             $('#dg2_2').datagrid('selectRow',index);// 关键在这里  
            var row = $('#dg2_2').datagrid('getSelected');  
            if (row){  
                $('#dd').show().dialog({ 
                    modal : true, 
                    title : '雇员详情信息', 
                    modal: true, 
                    closable: true, 
                    draggable: true, 
                    width: 180, 
                    height: 110
                    });
            }
            return false; 
     }
$(function(){
     host="http://10.128.33.182:8000/admin/";
         tb2_2={
        editRow:undefined,
        edit:function(){
            console.log("执行了编辑操作");

            //console.log($('#dg2_2').datagrid('getSelections')[0]);
          //var row=$('#dg2_2').datagrid('getSelections')[0];
            //console.log(row);
            var col=$('#dg2_2').datagrid('getColumnOption','workNum');
            console.log(col);
            col.editor={};
            //$('#dg2_2').datagrid('beginEdit',ii);
            //var ed = $('#dg2_2').datagrid('getEditor', {index:ii,field:'workNum'});
            
           // debugger;
            //var p=$(ed.target).closest('td[field]');//编辑器容器
            //p.html('');//删除原来的编辑器
            //依据product选中的内容生成对应的对象
           // if(value=='xxxx')p.datebox({xxxxxxx})
           // else p.numberbox({xxxxxx})
            var rows=$('#dg2_2').datagrid('getSelections');
            //问：rows是按选中的次序来排？
            if(rows.length==1){
                 if(tb2_2.editRow!=undefined){
                    console.log("此时已有其他行处于编辑状态");
                    $('#dg2_2').datagrid('endEdit',tb2_2.editRow);
                }
                if(tb2_2.editRow==undefined){
                   console.log("此时没有其他行处于编辑状态");
                   var rowIndex=$('#dg2_2').datagrid('getRowIndex',rows[0]);
                   $('#save2_2,#redo2_2').show();
                    $('#dg2_2').datagrid('beginEdit',rowIndex);
                    tb2_2.editRow=rowIndex;
                    //上一个点击了编辑的行，即使执行了endedit,但默仍为选中状态，
                    $('#dg2_2').datagrid('unselectRow',rowIndex);
                }
            }else{
                $.messager.alert("警告","修改只能选择一行",'warning');
            }
        },
        save:function(){
           // $('#save1_2,#hide').hide();
           // this.editRow=false;
            $('#dg2_2').datagrid('endEdit',this.editRow);
          
        },
        redo:function(){
            $('#save2_2,#redo2_2').hide();
            this.editRow=undefined;
             $('#dg2_2').datagrid('rejectChanges');
        }
    };


    $('#dg2_2').datagrid({
        url:host+'employee/list', 
        width:1000,
         toolbar:'#tb2_2',
        queryParams:{
           // start:0,
           // length:5
        },
        title:'雇员详情列表',
        fitColumns:true,
         columns:[[
            {   field:'workNum',
                title:'工号',
                width:60,
                editor:{
                    type:'text',
                    options:{
                        required:true,
                    }
                },
            },
           {
                field:'name',//field字段必须，为空也得写上，否则宽度为5px
                title:'姓名',
                width:100,
                editor:{
                    type:'validatebox',
                    options:{
                        required:true,
                    }
                },
               
           },
           {
                 field:'sexStr',
                title:'性别',
                width:50,
           },
            {   field:'sexId',
                title:'性别编号',
                width:50,
                editor: {  
                    type: 'combobox',  
                    options: {  
                        data: [{
                            text:'男',
                            value:'0',
                        },
                        {
                            text:'女',
                            value:'1',
                        }
                        ], 
                        valueField: 'value',  
                        textField: 'text',  
                         editable:false,
                        panelHeight: 'auto'  
                    }  
                },  
            },
             {   field:'birthday',
                title:'出生日期',
                width:100,
                formatter: formatDatebox,
               editor: 'datebox'
            },
             {   field:'nativePlace',
                title:'户籍',
                width:100,
                editor:{
                    type:'validatebox',
                    options:{
                        required:true,
                    }
                },
            },
            {
                field:'statusStr',
                title:'状态',
                width:80,
            },
             {   field:'statusId',
                title:'状态编号',
                width:80,
                editor: {  
                    type: 'combobox',  
                    options: {  
                       url:host+'status/list',
                        valueField: 'id',  
                        textField: 'name',  
                        panelHeight: 'auto',
                        editable:false,
                        required:true,  
                    }  
                },  
            },
             {   field:'hireDate',
                title:'雇用日期',
                width:100,
                formatter: formatDatebox,
                editor: 'datebox'
            },
             {   field:'departureDate',
                title:'离职日期',
                width:100,
                formatter: formatDatebox,
                editor: 'datebox'
            },
            {
                 field:'departmentStr',
                title:'所属部门',
                width:100,
            },
             {   field:'departmentId',
                title:'所属部门编号',
                width:100,
                editor: {  
                    type: 'combobox',  
                    options: {  
                       url:host+'department/list',
                        valueField: 'id',  
                        textField: 'name',  
                        panelHeight: 'auto',
                        editable:false,
                        required:true,  
                    }  
                },  
            },
             {   field:'xx',
                title:'操作',
                width:100,
                formatter: formatButton,
            },
        ]],
       
        pagination:true,
        pageSize:5,
        pageList:[5,10,15],
        pageNumber:1,
        onSelectPage:function(pageNumber,pageSize){
            console.log(pageNumber+":"+pageSize);
        },

         //在endEdit后自动触发检验是否符合validate才会执行
        onAfterEdit:function(rowIndex,rowData,changes){
                $('#save2_2,#redo2_2').hide();
                var updated=$('#dg2_2').datagrid('getChanges','updated');
                //新增用户
                if(updated.length>0){
                    console.log(updated);
                         $.ajax({
                           url: host+'employee/set',
                           type: 'POST',
                           dataType: 'json',
                           data: {
                                    workNum: updated[0].workNum,
                                   name:updated[0].name,
                                   sexId:updated[0].sexId,
                                   birthday: Date.parse(updated[0].birthday),
                                   nativePlace: updated[0].nativePlace,
                                   statusId:updated[0].statusId,
                                   hireDate: Date.parse(updated[0].hireDate),
                                   departureDate: Date.parse(updated[0].departureDate),
                                   departureId: updated[0].departmentId,
                                },
                       })
                       .done(function(data) {
                            console.log(data);
                            if(data.status==200){
                                 console.log("success");
                                 $('#dg2_2').datagrid('loaded');
                                 $('#dg2_2').datagrid('load');
                                
                                 $.messager.show({
                                    title:'提示',
                                    msg:"您已成功修改",
                                 });
                                  tb2_2.editRow=undefined;
                             }else if(data.status==301){
                              $('#dg2_2').datagrid('reload');
                               $.messager.alert("警告，修改失败！","该用户不存在",'warning');
                             }else if(data.status==302){
                                $('#dg2_2').datagrid('reload');
                               $.messager.alert("警告，修改失败！","外键依赖关系不满足",'warning');
                             }
                          
                       })
                       .fail(function() {
                        $('#dg2_2').datagrid('reload');
                            $.messager.alert("警告，添加失败！","不明错误",'warning');
                       })
                       .always(function() {
                           console.log("complete");
                       });

                }
                console.log("1111");
                 $('#dg2_2').datagrid('unselectAll');
                 tb2_2.editRow=undefined;
               
        },//end  onAfterEdit:
        onDblClickRow:function(rowIndex,rowData){
            console.log("执行了双击操作");
            //console.log(rowData);
            if(tb2_2.editRow!=undefined){
                console.log("此时已有其他行处于编辑状态");
                $('#dg2_2').datagrid('endEdit',tb2_2.editRow);

            }
            if(tb2_2.editRow==undefined){
               console.log("此时没有其他行处于编辑状态");
               $('#save2_2,#redo2_2').show();
                $('#dg2_2').datagrid('beginEdit',rowIndex);
                tb2_2.editRow=rowIndex;
            }
        }//end ondblclickrow
    });

     function formatButton(val,row,index){
       // alert("");
        return '<a href="#" onclick="editUser('+index+')">显示详情</a>'
     }

     function formatDatebox(value) {
            if (value == null || value == '') {
                return '';
            }
            var dt;
            if (value instanceof Date) {
                dt = value;
            } else {

                dt = new Date(value);

            }

            return dt.format("yyyy-MM-dd"); //扩展的Date的format方法(上述插件实现)
        }
        $.extend($.fn.datagrid.defaults.editors, {
        datebox : {
        init : function(container, options) {
            var input = $('<input type="text">').appendTo(container);
            input.datebox(options);
            return input;
        },
        destroy : function(target) {
            $(target).datebox('destroy');
        },
        getValue : function(target) {
            return $(target).datebox('getValue');//获得旧值
        },
        setValue : function(target, value) {
            console.info(formatDatebox(value));
            $(target).datebox('setValue', formatDatebox(value));//设置新值的日期格式
        },
        resize : function(target, width) {
            $(target).datebox('resize', width);
        }
        }
        });
});
